﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
//
using DLLTest.OpertionClass;
using System.Data.OleDb;

namespace DLLTest
{
    public partial class frm_DataBase : Form
    {
        Action refreshTreeAction;//激活主窗口
        DataOperate dataoperate = new DataOperate();
        int Status = 0;//checbox状态
        string id = "";//id号
        /// <summary>
        /// 检查TextBox的Text属性是否为空或没有字符
        /// </summary>
        /// <param name="sender"></param>
        /// <returns>true/false</returns>
        private bool TextBoxValidator(object sender)
        {
            if (String.IsNullOrEmpty(((TextBox)sender).Text))
                return true;
            else
                return false;
        }
        //
        private frm_DataBase()
        {
            InitializeComponent();
        }

        /// <summary>
        /// 刷新主界面中的树
        /// </summary>
        /// <param name="refreshTree">刷新主界面中的树</param>
        public frm_DataBase(Action UpTree)
        {
            InitializeComponent();
            this.refreshTreeAction = UpTree;
        }

        /// <summary>
        /// 新增树信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnAdd_Click(object sender, EventArgs e)
        {
            string insertSqlCmd = string.Empty;
            if (TextBoxValidator(txtParentID))
            {
                MessageBox.Show("请输入ParentID(父节点)", "提示");
                txtParentID.Focus();
            }
            else if (TextBoxValidator(txtTreeName))
            {
                MessageBox.Show("请输入地址或通道号", "提示");
                txtTreeName.Focus();
            }
            else if (TextBoxValidator(txtTreeText))
            {
                MessageBox.Show("请输入设备名称", "提示");
                txtTreeText.Focus();
            }
            else if (TextBoxValidator(txtDriverName))
            {
                MessageBox.Show("请输入用户名", "提示");
                txtDriverName.Focus();
            }
            else if (TextBoxValidator(txtDriverPwd))
            {
                MessageBox.Show("请输入密码", "提示");
                txtDriverPwd.Focus();
            }
            else if (txtDriverPwd2.Text != txtDriverPwd.Text)
            {
                MessageBox.Show("两次输入的密码不一致", "提示");
                txtDriverPwd2.Text = "";
                txtDriverPwd2.Focus();
            }
            else if (TextBoxValidator(txtDriverPort))
            {
                MessageBox.Show("请输入通道号", "提示");
                txtDriverPort.Focus();
            }           
            else
            {
                if (cbStatus.Checked == false)
                {
                    Status = 0;
                }
                else
                {
                    Status = 1;
                }
                //数据库增加
                string sql = "select * from TreeNode where TreeName='" + txtTreeName.Text.Trim() + "'";
                DataTable dt=dataoperate.GetDataSet(sql).Tables[0];
                if (dt.Rows.Count > 0)
                {
                    MessageBox.Show("该地址已经存在","信息提示");//
                }
                else
                {
                    //MessageBox.Show("无此地址，可以添加");                   
                   //新增
                    AddTreeNode();
                    frm_DataBase_Load(null, null);
                    if (this.refreshTreeAction != null)
                        this.refreshTreeAction();
                }
                
            }
        }

        private void frm_DataBase_Load(object sender, EventArgs e)
        {
            DataSet treenode=dataoperate.GetDataSet();
            dGView1.DataSource = treenode.Tables[0];
            btnEdit.Enabled = false;
            btnAdd.Enabled = true;
            btnDell.Enabled = false;
        }

        private void dGView1_DoubleClick(object sender, EventArgs e)
        {
            txtDriverPwd2.Text = "";//设置确认密码为空值
            txtParentID.Text = dGView1[1, dGView1.CurrentCell.RowIndex].Value.ToString();
            if (txtParentID.Text == "1")
            {
                //一级节点
                //设置文本框和按钮的readOnly属性
                txtParentID.ReadOnly = false;
                txtParentID.BackColor = SystemColors.Window;
                txtDriverName.ReadOnly = false;
                txtDriverName.BackColor = SystemColors.Window;
                txtDriverPwd.ReadOnly = false;
                txtDriverPwd.BackColor = SystemColors.Window;
                txtDriverPwd2.ReadOnly = false;
                txtDriverPwd2.BackColor = SystemColors.Window;
                txtDriverPort.ReadOnly = false;
                txtDriverPort.BackColor = SystemColors.Window;
               //button显示设置
                btnAdd.Enabled = false;
                btnEdit.Enabled = true;
                //
                id = dGView1[0, dGView1.CurrentCell.RowIndex].Value.ToString();
                txtTreeName.Text = dGView1[2, dGView1.CurrentCell.RowIndex].Value.ToString();
                txtTreeText.Text = dGView1[3, dGView1.CurrentCell.RowIndex].Value.ToString();
                txtDriverName.Text = dGView1[4, dGView1.CurrentCell.RowIndex].Value.ToString();
                txtDriverPwd.Text = dGView1[5, dGView1.CurrentCell.RowIndex].Value.ToString();
                txtDriverPort.Text = dGView1[6, dGView1.CurrentCell.RowIndex].Value.ToString();
                txtChannelNo.Text = dGView1[7, dGView1.CurrentCell.RowIndex].Value.ToString();
                string status = dGView1[8, dGView1.CurrentCell.RowIndex].Value.ToString();
                if (status == "1")
                {
                    cbStatus.Checked = true;
                }
                else
                {
                    cbStatus.Checked = false;
                }
                //更新一级节点
            }
            else 
            {
                //二级节点
                //恢复需要更改项
                txtChannelNo.ReadOnly = false;
                //不能更改项，父id，用户名，密码，确认密码，端口号
                txtParentID.ReadOnly = true;
                txtParentID.BackColor = SystemColors.ScrollBar;
                txtDriverName.ReadOnly = true;
                txtDriverName.BackColor = SystemColors.ScrollBar;
                txtDriverPwd.ReadOnly = true;
                txtDriverPwd.BackColor = SystemColors.ScrollBar;
                txtDriverPwd2.ReadOnly = true;
                txtDriverPwd2.BackColor = SystemColors.ScrollBar;
                txtDriverPort.ReadOnly = true;
                txtDriverPort.BackColor = SystemColors.ScrollBar;
                //button显示设置
                btnAdd.Enabled = false;
                btnEdit.Enabled = true;
                //设置不能更改项为空值
                txtParentID.Text="";
                txtDriverName.Text = "";
                txtDriverPwd.Text = "";
                txtDriverPwd2.Text = "";
                txtDriverPort.Text = "";
                txtDriverPwd2.Text = "";
                //
                id = dGView1[0, dGView1.CurrentCell.RowIndex].Value.ToString();
                txtParentID.Text = dGView1[1, dGView1.CurrentCell.RowIndex].Value.ToString();
                txtTreeName.Text = dGView1[2, dGView1.CurrentCell.RowIndex].Value.ToString();
                txtTreeText.Text = dGView1[3, dGView1.CurrentCell.RowIndex].Value.ToString();
                txtChannelNo.Text = dGView1[7, dGView1.CurrentCell.RowIndex].Value.ToString();
                string status = dGView1[8, dGView1.CurrentCell.RowIndex].Value.ToString();
                if (status == "1")
                {
                    cbStatus.Checked = true;
                }
                else
                {
                    cbStatus.Checked = false;
                }             
                //更新二级节点
            }           
        }

        /// <summary>
        /// 新增树信息
        /// </summary>
        private void AddTreeNode()
        {
            OleDbDataAdapter sda = dataoperate.sqlsda("select * from TreeNode");
            OleDbCommandBuilder sb = new OleDbCommandBuilder(sda);
            DataSet dst = dataoperate.GetDataSet();
            sda.Fill(dst, "TreeNode");
            if (txtParentID.Text == "1")//添加一级节点
            {
                //MessageBox.Show("添加IP地址");
                try
                {
                    DataRow newrow = dst.Tables["TreeNode"].NewRow();//添加一个新数据行
                    //开始添加
                    newrow["ParentID"] = txtParentID.Text.Trim();
                    newrow["TreeName"] = txtTreeName.Text.Trim();
                    newrow["TreeText"] = txtTreeText.Text.Trim();
                    newrow["DriverName"] = txtDriverName.Text.Trim();
                    newrow["DriverPwd"] = txtDriverPwd.Text.Trim();
                    newrow["DriverPort"] = txtDriverPort.Text.Trim();
                    //newrow["ExceptChannel"] = txtChannelNo.Text.Trim();
                    if (cbStatus.Checked == true)
                    {
                        newrow["Status"] = 1;
                    }
                    else
                    {
                        newrow["Status"] = 0;
                    }
                    //结束添加
                    dst.Tables["TreeNode"].Rows.Add(newrow);
                    sda.Update(dst, "TreeNode");
                    dst.Dispose();
                    MessageBox.Show("数据新增成功！");
                }
                catch { MessageBox.Show("数据新增失败！"); }
            }
        }

        /// <summary>
        /// 修改树信息
        /// </summary>
        private void EditTreeNode()
        {
            #region 修改
            OleDbDataAdapter sda = dataoperate.sqlsda("select * from TreeNode where ID=" +id);
            DataSet dst = new DataSet();
            OleDbCommandBuilder sb = new OleDbCommandBuilder(sda);
            sda.Fill(dst, "TreeNode");
            if (txtParentID.Text == "1")//一级节点
            {
                if (txtDriverPwd.Text != txtDriverPwd2.Text)
                {
                    MessageBox.Show("两次密码不一致，请重新输入");
                    return;
                }
                try
                {  
                    //开始修改 
                    dst.Tables["TreeNode"].Rows[0]["ParentID"] = this.txtParentID.Text.Trim();
                    dst.Tables["TreeNode"].Rows[0]["TreeName"] = this.txtTreeName.Text;
                    dst.Tables["TreeNode"].Rows[0]["TreeText"] = this.txtTreeText.Text;
                    dst.Tables["TreeNode"].Rows[0]["DriverPwd"] = this.txtDriverPwd.Text;
                    dst.Tables["TreeNode"].Rows[0]["DriverPort"] = this.txtDriverPort.Text;
                    dst.Tables["TreeNode"].Rows[0]["ExceptChannel"] = this.txtChannelNo.Text;
                    //状态更新
                    if (cbStatus.Checked == true)
                    {
                        dst.Tables["TreeNode"].Rows[0]["Status"] = 1;
                    }
                    else
                    {
                        dst.Tables["TreeNode"].Rows[0]["Status"] = 0;
                    }                    
                    //结束修改
                    sda.Update(dst, "TreeNode");
                    dst.Dispose();
                    MessageBox.Show("数据修改成功！");
                }
                catch
                {
                    MessageBox.Show("数据修改失败！");
                }
            }
            #endregion
        }

        /// <summary>
        /// 删除树信息
        /// </summary>
        private void DellTreeNode()
        {
            if (id != "1")
            {
                string sql = "select * from TreeNode where ParentID='" + id + "'";
                DataTable dt = dataoperate.GetDataSet(sql).Tables[0];
                if (dt.Rows.Count > 0)
                {
                    MessageBox.Show("该地址已经关联通道，必须将该地址下所有通道删除才能删除该地址", "信息提示");
                }
                else
                {
                    //MessageBox.Show("该地址下没有通道", "信息提示");
                    DataOperate dataoperate2 = new DataOperate();
                    string sql2 = "select * from TreeNode where Status=" + 1 + " and ID=" + Convert.ToInt32(id);
                    DataSet ds2 =dataoperate2.GetDataSet(sql2);
                    DataTable dt2 = ds2.Tables[0];
                    if (dt2.Rows.Count > 0)
                    {
                        MessageBox.Show("该通道在播放中，不能删除，必须停止播放，并且修改使用状态", "信息提示");
                       
                    }
                    else
                    {
                        //MessageBox.Show("该通道可以删除", "信息提示");
                        if (dataoperate.DellTree("TreeNode", Convert.ToInt32(id)) == true)
                        {
                            MessageBox.Show("删除成功", "信息提示");
                            frm_DataBase_Load(null,null);
                        }
                        else
                        { MessageBox.Show("删除失败", "信息提示"); }
                    }
                }
            }
        }

        /// <summary>
        /// 修改数据库信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnEdit_Click(object sender, EventArgs e)
        {
            //修改
            EditTreeNode();
            frm_DataBase_Load(null,null);
            if (this.refreshTreeAction != null)
                this.refreshTreeAction();
        }

        /// <summary>
        /// 删除数据库信息
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnDell_Click(object sender, EventArgs e)
        {
            DellTreeNode();
            if (this.refreshTreeAction != null)
                this.refreshTreeAction();
        }

        /// <summary>
        /// dbgrid单击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void dGView1_Click(object sender, EventArgs e)
        {
            //获取id号
            id = dGView1[0, dGView1.CurrentCell.RowIndex].Value.ToString();
            //MessageBox.Show(id);
            btnDell.Enabled = true;
            UPTxt();
            
        }

        /// <summary>
        /// 设置文本控件为空值
        /// </summary>
        private void UPTxt()
        {
            txtChannelNo.Text = "";
            txtDriverName.Text = "";
            txtDriverPort.Text = "";
            txtDriverPwd.Text = "";
            txtDriverPwd2.Text = "";
            txtParentID.Text = "";
            txtTreeName.Text = "";
            txtTreeText.Text = "";
            cbStatus.Checked = false;
        }
    }
}
